	package manong.serviceimpl;

	import com.github.pagehelper.PageHelper;
	import com.github.pagehelper.PageInfo;
	import manong.constant.ServerResponse;
	import manong.beans.Stu;
	import manong.beans.StuQuery;
	import manong.mapper.StuMapper;
	import manong.service.IStuService;
	import org.apache.ibatis.session.RowBounds;
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

	@Service
	public class StuServiceImpl implements IStuService {
	    @Autowired
	    private StuMapper stuMapper;

	    @Override
	    public ServerResponse<String> add(String studentName, String className,Integer sno,String mark,String studentPhone,String speciality) {
	        StuQuery query = new StuQuery();
	        query.createCriteria().andStudentNameEqualTo(studentName);
	        Stu stu = stuMapper.selectOneByExample(query);
	        if (stu != null) {
	            return ServerResponse.createByErrorMessage("该学生已经存在");
	        }
	        Stu s = new Stu();
	        s.setStudentName(studentName);
	        s.setClassName(className);
	        s.setSno(sno);
	        s.setMark(mark);
	        s.setStudentPhone(studentPhone);
	        s.setSpeciality(speciality);
	        int count = stuMapper.insertSelective(s);
	        if (count > 0) {
	            return ServerResponse.createBySuccessMessage("新建成功");
	        }
	        return ServerResponse.createByErrorMessage("新建失败");
	    }






	    @Override
	    public ServerResponse<String> del(Integer id) {
	        int count = stuMapper.deleteByPrimaryKey(id);
	        if (count > 0) {
	            return ServerResponse.createBySuccessMessage("删除成功");
	        }
	        return ServerResponse.createByErrorMessage("删除失败");
	    }




	    @Override
	    public ServerResponse<String> update(Integer id, String studentName,String className,Integer sno,String mark,String studentPhone,String speciality) {
	        Stu stu = stuMapper.selectByPrimaryKey(id);
	        stu.setStudentName(studentName);
	        stu.setClassName(className);
	        stu.setSno(sno);
	        stu.setMark(mark);
	        stu.setStudentPhone(studentPhone);
	        stu.setSpeciality(speciality);
	        int count = stuMapper.updateByPrimaryKeySelective(stu);
	        if (count > 0) {
	            return ServerResponse.createBySuccessMessage("更新成功");
	        }
	        return ServerResponse.createByErrorMessage("更新失败");
	    }

	    @Override
	    public ServerResponse<Stu> find(Integer id) {
	        return ServerResponse.createBySuccess(stuMapper.selectByPrimaryKey(id));
	    }

	    @Override
	    public ServerResponse<PageInfo<Stu>> findAll(Integer pageNum, Integer pageSize) {
	    	StuQuery query = new StuQuery();
	        PageHelper.startPage(pageNum, pageSize);
	        List<Stu> list = stuMapper.selectByExampleWithRowbounds(query,new RowBounds((pageNum-1)*10,pageSize));
	        PageInfo<Stu> pageInfo = new PageInfo<>(list);
	        pageInfo.setTotal(stuMapper.countByExample(query));
	        return ServerResponse.createBySuccess(pageInfo);
	    }

		@Override
		public ServerResponse<PageInfo<Stu>> findByClassRom(String Sno, Integer pageNum, Integer pageSize) {
			//查询当前的学生的班级
			StuQuery query = new StuQuery();
			query.createCriteria().andSnoEqualTo(Integer.parseInt(Sno));
			Stu stu = stuMapper.selectOneByExample(query);
			Integer classid = stu.getClassid();

			//根据教室id查询学生
			StuQuery query1 = new StuQuery();
			query1.createCriteria().andClassidEqualTo(classid);
			PageHelper.startPage(pageNum, pageSize);
			List<Stu> list = stuMapper.selectByExampleWithRowbounds(query1,new RowBounds((pageNum-1)*10,pageSize));
			PageInfo<Stu> pageInfo = new PageInfo<>(list);
			pageInfo.setTotal(stuMapper.countByExample(query1));
			return ServerResponse.createBySuccess(pageInfo);
		}
	}



